package com.sduept.nwld.secondaryequipment.evaluation.rest;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sduept.bigdata.risk.enums.RiskLevel;
import com.sduept.bigdata.risk.service.RiskAlarmMessageQueryManager;
import com.sduept.bigdata.risk.service.RiskAssessmentBrowserManager;
import com.sduept.bigdata.risk.service.RiskAssessmentManager;


/**
 * 电网二次保护装置进行风险评估   Rest服务
 *
 */
@RestController
@RequestMapping("/protectRiskAssment")
public class ProtectRiskAssmentServices {

	@Autowired
	private RiskAssessmentManager ram;
	@Autowired
	private RiskAssessmentBrowserManager rabm;
	@Autowired
	private RiskAlarmMessageQueryManager  ramqm;
	
	private RiskLevel[] levels = new RiskLevel[]{RiskLevel.DefaultLevel,RiskLevel.HighLevel,RiskLevel.MiddleLevel,RiskLevel.LowLevel};
	
	/**
	 * 侧边栏二次设备评估用到
	 * @return
	 */

	@GetMapping("/getAllSecondaryAssmentCount")
	public String getAllSecondaryAssmentCount(){
		Map<String, Integer> pAss = ram.getProtectionSecondaryAssmentCountByStationId(null);
		Map<String, Integer> sAss = ram.getStationSecondaryAssmentCount(null);
		JSONObject[] sum = new JSONObject[levels.length];
		JSONObject[] pPieData = new JSONObject[levels.length];
		JSONObject[] sPieData = new JSONObject[levels.length];
		for(int i=0;i<levels.length;i++){
			Integer pv = pAss.get(levels[i].getLabel());
			Integer sv = sAss.get(levels[i].getLabel());
			sum[i] = getSumData(levels[i],sv,pv);
			pPieData[i] = getPieData(levels[i],pv);
			sPieData[i] = getPieData(levels[i],sv);
		}
		JSONObject result = new JSONObject();
		result.put("protectionPieData", pPieData);
		result.put("sum", sum);
		result.put("stationPieData", sPieData);
		return result.toJSONString();
	}
	private JSONObject getSumData(RiskLevel level,Integer svalue,Integer pvalue){
		JSONObject res = new JSONObject();
		res.put("name", level.getLabel());
		res.put("sValue", svalue);
		res.put("pValue", pvalue);
		res.put("color", level.getColor());
		return res;
	}
	private JSONObject getPieData(RiskLevel level,Integer value){
		JSONObject res = new JSONObject();
		JSONObject itemStyle = new JSONObject();
		JSONObject normal = new JSONObject();
		normal.put("color", level.getColor());
		itemStyle.put("normal", normal);
		res.put("name", level.getLabel());
		res.put("value", value);
		res.put("itemStyle", itemStyle);
		return res;
	}
	/**
	 * 侧边栏二次设备评估用到
	 * @return
	 */

	@GetMapping("/getRiskLevels")
	public String getRiskLevels(){
		JSONArray sum = new JSONArray();
		for(int i=0;i<levels.length;i++){
			sum.add(getSumData(levels[i],null,null));
		}
		return sum.toJSONString();
	}
	/**
	 * 保护状态告警地图展示模块（当需要全网数据时调用，暂时没用）
	 * @return
	 */

	@GetMapping("/getGridSecondaryEvaluation")
	public String getGridSecondaryEvaluation(){
		JSONObject result = rabm.getProtectionStatusEvaluationBrowserData(null,null);
		return result.toJSONString();
	}
	/**
	 * 保护状态告警弹窗
	 * @param msgIds
	 * @return
	 */

	@GetMapping("/getAlarmMsgByIds/{msgIds}")
	public String getAlarmMsgByIds(@PathVariable("msgIds") String msgIds){
		JSONArray result = new JSONArray();
		if(StringUtils.isNoneEmpty(msgIds)){
			String[] ids = msgIds.split(",");
			result = rabm.getProtectionStatusMsgBrowserData(ids);
		}
		return result.toJSONString();
	}
}
